package com.gzu.shop.recommend;

import com.gzu.shop.entity.ThingSimilarity;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 离线推荐，对用户过去100条浏览记录进行推荐
 */

public class OfflineRecommend {
    @Scheduled(cron = "0 0 3 * * ?") // 每天晚上3点执行一次
    public static List<Long> updateRecommend(List<Long> userRelaThingId, List<ThingSimilarity> thingSimilarities) {
        //根据用户历史行为的商品id,userRelaThingId, 在thingSimilarities中相似度高的，推荐
        List<Long> recommendThingId = new ArrayList<>();
//        if (userRelaThingId.size() == 0) {
//            //如果用户没有浏览记录，则推荐全部商品
//            return allGoodsId;
//        }
        //对thingSimilarities 按照相似度降序排序
        thingSimilarities.sort((o1, o2) -> o2.getSimilarity().compareTo(o1.getSimilarity()));

        for (Long thingId : userRelaThingId) {
            for (ThingSimilarity thingSimilarity : thingSimilarities) {
                if (thingSimilarity.getThing1Id().equals(thingId) || thingSimilarity.getThing2Id().equals(thingId)) {
                    if (thingSimilarity.getThing1Id().equals(thingId)) {
                        recommendThingId.add(thingSimilarity.getThing2Id());
                    }
                    else {
                        recommendThingId.add(thingSimilarity.getThing1Id());
                    }

                }
            }
        }
        // 如果大于10个商品，取相似度最高的10个商品
        if (recommendThingId.size() > 10) {
            recommendThingId = recommendThingId.subList(0, 10);
        }

        return recommendThingId;
    }
}
